package com.wangsd.common.component;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.google.common.collect.Maps;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class WxjavaService {

    private static Map<String, WxMaService> maServices = Maps.newHashMap();

    private static Map<String, WxMpService> mpServices = Maps.newHashMap();


    /**
     * 获取公众号服务类
     *
     * @param appid
     * @return
     */
    public WxMpService getMpService(String appid, String secret) {
        WxMpService wxMpService = mpServices.get(appid);
        if (wxMpService == null) {
            wxMpService = new WxMpServiceImpl();
            WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
            config.setAppId(appid);
            config.setSecret(secret);
            wxMpService.setWxMpConfigStorage(config);
            mpServices.put(appid, wxMpService);
        }
        return wxMpService;
    }
}
